<?php

namespace app\common\model\xiluedu;

use think\Model;
use traits\model\SoftDelete;
use function fast\array_get;

class LotteryActive extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'xiluedu_lottery_active';

    protected $autoWriteTimestamp = 'int';

    protected $deleteTime = 'deletetime';
    // 追加属性
    protected $append = [
        'join_type_text',
        'start_time_text',
        'end_time_text'
    ];
    

    
    public function getJoinTypeList()
    {
        return ['1' => __('Join_type 1'), '2' => __('Join_type 2')];
    }

    public function getJoinTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['join_type']) ? $data['join_type'] : '');
        $list = $this->getJoinTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    /**
     * 抽奖活动列表
     * @param $params
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public static function lists($params){
        $pagesize = array_get($params,'pagesize',10);

        return static::field('id,name,image,start_time,end_time')
            ->where('status','1')
            ->order('start_time','desc')
            ->paginate($pagesize);
    }

    /**
     * 抽奖活动详情
     * @param $params
     * @return array|bool|false|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function detail($params){
        $lottery_active_id = array_get($params,'lottery_active_id');
        if(!$lottery_active_id){
            $where['id'] = $lottery_active_id;
        }else{
            $where['end_time'] = ['gt',time()];
        }
        return static::with(['lotteryAwards'])->where($where)
            ->order('start_time','asc')
            ->find();
    }


    public function lotteryAwards(){
        return $this->hasMany('LotteryAwards','lottery_active_id','id')->where('status','1');
    }
}
